package speedball.sound;

import javafx.collections.ObservableList;
import javafx.scene.Group;
import javafx.scene.Node;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.shape.Rectangle;
import javafx.stage.Stage;

import javax.sound.sampled.*;

import java.awt.*;
import java.io.File;


public class MusiclineFx extends Stage
{

   private  Scene scene;
    private Group group;
    FFT fft=new FFT();
    private byte[] audioData;
    private int intBytes = 0;
    private byte[] ml=new byte[1];
    private int[] drawl;

    private Rectangle[] rectangles;

    /** Creates new form Musicline */
    public MusiclineFx()
    {
        group=new Group();
        ObservableList<Node> children = group.getChildren();
        rectangles=new Rectangle[100];
        for (int i = 0; i < 100; i++) {
            rectangles[i]=new Rectangle();
            children.add(rectangles[i]);
        }
        javafx.scene.control.Button Button=new Button("fdsfsfsd");
        children.add(Button);
        scene=new Scene(group);
        setScene(scene);
        setWidth(1000);
        setHeight(400);
//        for (Rectangle rectangle:rectangles) {
//        rectangle=new Rectangle();
//        }
//        initComponents();
//        Graphics g;
//        g=this.getGraphics();
    }
    public void paint(Graphics g)
    {
//        int width=this.getWidth()/256;
//        g.clearRect(0,0,1000,900);
//
//        g.setColor(Color.BLACK);
        //System.out.print(drawl.length);
        if(audioData!=null)
        {
            drawl=new int[audioData.length];
            for(int i=0;i<audioData.length;i++)
            {
                ml=audioData;
                //String s=new String(ml);
                drawl[i]=Math.abs(ml[i]);
//                drawl[i] = ml[i] < 0 ? 127 : abs;
            }
//            fft.calculate(drawl);
            System.out.println(drawl[0]);
            for(int i=0;i<drawl.length-1;i++)
            {
//                g.drawLine(i*this.getWidth()/256,drawl[i]+100,(i+1)*this.getWidth()/256,drawl[i+1]+100);
//                g.drawRect(i*width,100,width,drawl[i]);
            }
        }

    }
    public void run()
    {
        while(intBytes!=-1)
        {
            try
            {
                synchronized(this)
                {
                    this.wait(50);
                }
            } catch (InterruptedException ex)
            {
                ex.printStackTrace();
            }
//            repaint();
        }
    }
    public Rectangle[] play()
    {
        try
        {
//            AudioInputStream ais = AudioSystem.getAudioInputStream(new File( "E:\\ideaproject\\speedball\\src\\main\\java\\speedball\\sound\\presence_changed.wav"));// 获得音频输入流
            AudioInputStream ais = AudioSystem.getAudioInputStream(new File( "E:\\ideaproject\\speedball\\src\\main\\java\\speedball\\sound\\suojing-liaotiao.wav"));// 获得音频输入流
            ais=AudioSystem.getAudioInputStream(AudioFormat.Encoding.PCM_SIGNED,ais);
            AudioFormat baseFormat = ais.getFormat();// 指定声音流中特定数据安排
            System.out.println("baseFormat="+baseFormat);
            DataLine.Info info = new DataLine.Info(SourceDataLine.class, baseFormat);
            System.out.println("info="+info);
            SourceDataLine line = (SourceDataLine) AudioSystem.getLine(info);
            // 从混频器获得源数据行
            System.out.println("line="+line);
            line.open(baseFormat);// 打开具有指定格式的行，这样可使行获得所有所需的系统资源并变得可操作。
            line.start();// 允许数据行执行数据 I/O
            int BUFFER_SIZE = 256;

            audioData = new byte[BUFFER_SIZE];
            int s=5;
            int s2=3;
            int df=10;
            while (intBytes != -1&&df<=45)
            {
                intBytes = ais.read(audioData, 0, BUFFER_SIZE);// 从音频流读取指定的最大数量的数据字节，并将其放入给定的字节数组中。
                Thread.sleep(50);
                System.out.println("正在执行");
                df++;
                for (int i=0;i<rectangles.length;i++) {
                    Rectangle rectangle =rectangles[i];
                    rectangle.setLayoutX(s*i);
                    rectangle.setLayoutY(100D);
                    rectangle.setWidth(s2);
                    rectangle.setHeight(Math.abs(Double.valueOf(audioData[i])));
                    System.out.println(Math.abs(Double.valueOf(audioData[i])));
                }

//                if (intBytes >= 0)
//                {
//                    int outBytes = line.write(audioData, 0, intBytes);// 通过此源数据行将音频数据写入混频器。
//                }
            }

        } catch (Exception e)
        {
            e.printStackTrace();
        }
        return rectangles;
    }
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
// <editor-fold defaultstate="collapsed" desc=" 生成的代码 ">
//    private void initComponents()
//    {
//        setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
//        GroupLayout layout = new GroupLayout(getContentPane());
//        getContentPane().setLayout(layout);
//        layout.setHorizontalGroup(
//                layout.createParallelGroup(GroupLayout.Alignment.LEADING).addGap(0, 1000, Short.MAX_VALUE));
//        layout.setVerticalGroup(layout.createParallelGroup(GroupLayout.Alignment.LEADING) .addGap(0, 1000, Short.MAX_VALUE));
//        pack();
//    }// </editor-fold>

    /**
     * @param args the command line arguments
     */
    public static void main(String args[])
    {
        MusiclineFx msl=new MusiclineFx();
        msl.show();
//        Thread yh=new Thread(msl);
//        yh.start();
        msl.play();
    }

}




